// pages/note/note.js
import { wxRequest } from "../../utils/ajax";
Page({
  /**
   * 组件的初始数据
   */
  data: {
    list:[],
    // list: [{ "noteId": "1", "title": "行星星云大会" }, { "noteId": "2","title": "行星星云大会" }],
    pageStart: 1,
    pageTotal: 5,
    paging: true,
  },
  onLoad: function () {
    this.getNotes(this.data.pageStart, this.data.pageTotal);
  },
  //tab
  getCurrentTab: function (e) {
    // console.log(e);
    let that = this;
   
    that.getNotes(1, that.data.pageTotal);
  },
  getNotes: function (pageStart, pageTotal) {
    let that = this;

    wxRequest('/note/list', {
      pageStart: pageStart, pageTotal: pageTotal
    }, function (data) {
      console.log(data);
      let arr = that.data.list;
      let res = data.data.data;
      var tempList = res;

      if (pageStart == 1) {
        arr = [];
        that.setData({
          pageStart: pageStart
        });
      }
      if (res == null) {
        that.setData({
          paging: false,
          list: null,
          pageStart: 1
        });
      } else {
        if (res.length < pageTotal) {
          that.setData({
            paging: false,
            list: arr.concat(tempList),
          });
        } else {
          that.setData({
            paging: true,
            pageStart: pageStart + 1,
            list: arr.concat(tempList),
          });
        }
    }
    });
  },
  bindNoteDetail: function (e) {
    let noteId = e.currentTarget.dataset.id;
    console.log(noteId);
    if (noteId != "" && noteId != undefined) {
      wx.navigateTo({
        url: '/pages/note/detail?artId=' + noteId
      })
    }
  },
  onReachBottom: function () {
    let that = this;
    if (that.data.paging) {
      that.getNotes(that.data.pageStart, that.data.pageTotal);
    }
  }
})
